package com.wpen.unit.config;

import org.springdoc.core.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

/**
 * SpringDoc API文档相关配置
 * 
 * @author Wang Peng
 * @date 2022-12-06
 */
@Configuration
public class SpringDocConfig {
    @Value("${wpen.application.doc.title}")
    private String title;
    @Value("${wpen.application.doc.version}")
    private String version;
    @Value("${server.servlet.context-path}")
    private String security_name;

    @Bean
    public OpenAPI mallTinyOpenAPI() {
        return new OpenAPI()
                .info(new Info().title(title.toUpperCase() + " API")
                        .description(title.toUpperCase() + " API [SpringDoc]")
                        /**/
                        .version(version)
                        /**/
                        .contact(new Contact().name("wangpeng"))
                        /**/
                        .license(new License().name("Apache 2.0").url("")))
                .externalDocs(new ExternalDocumentation().description("SpringBoot文档")
                //.url("http://www.smatm.com")
                ).addSecurityItem(new SecurityRequirement().addList(security_name))
                .components(new Components().addSecuritySchemes(security_name,
                        new SecurityScheme().name(security_name).type(SecurityScheme.Type.HTTP)// .scheme("bearer")
                                .bearerFormat("JWT")));
    }

//    @Bean
//    public GroupedOpenApi publicApi() {
//        return GroupedOpenApi.builder().group("brand").pathsToMatch("/brand/**").build();
//    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder().group("admin").pathsToMatch("/**").build();
    }
}